package com.example.a10928.weifriend.ui.personal_center

import android.content.Intent
import android.graphics.Color
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v4.content.ContextCompat
import android.support.v4.widget.SwipeRefreshLayout
import android.support.v7.widget.LinearLayoutManager
import com.example.a10928.weifriend.App
import com.example.a10928.weifriend.R
import com.example.a10928.weifriend.adapter.MyWalletAdapter
import com.example.a10928.weifriend.adapter.RecommondAdapter
import com.example.a10928.weifriend.http.api.VariableName
import com.example.a10928.weifriend.http.api.base.WalletFlowBean
import com.example.a10928.weifriend.ui.BaseActivity
import com.example.a10928.weifriend.util.RxUtil
import com.example.a10928.weifriend.util.StatusBarUtil
import com.githang.statusbar.StatusBarCompat
import io.reactivex.functions.Consumer
import kotlinx.android.synthetic.main.activity_my_wallet.*
import java.util.ArrayList
import com.example.a10928.weifriend.ui.MainActivity
import com.example.a10928.weifriend.util.NumAnimUtils
import com.mylhyl.circledialog.CircleDialog



class MyWalletActivity : BaseActivity(),SwipeRefreshLayout.OnRefreshListener {


    var datas:MutableList<WalletFlowBean.MoneyFlowBean> = mutableListOf()
    var adapter:MyWalletAdapter?=null

    override fun getContentView(): Int {
        return R.layout.activity_my_wallet
    }

    override fun initView() {
        supportActionBar?.hide()
        StatusBarCompat.setStatusBarColor(this, Color.WHITE,true)
        adapter = MyWalletAdapter(R.layout.item_wallet, datas)
        recyc.setLayoutManager(LinearLayoutManager(this))
        recyc.setAdapter(adapter)

        adapter?.setOnItemClickListener { adapter, view, position ->

            CircleDialog.Builder()
                    .setTitle("内容")
                    .setText(datas[position].content)
                    .setPositive("确定", null)
                    .show(supportFragmentManager)

        }


        ll_withdraw.setOnClickListener {

            val items = arrayOf("支付宝", "银联")
            CircleDialog.Builder()
                    .setTitle("提现")
                    .setSubTitle("请选择提现方式")
                    .setItems(items) { parent, view1, position1, id ->
                        when(position1){
                            0->{
                                var intent= Intent(this,WithDrawActivity::class.java)
                                intent.putExtra("data",tv_total.text.toString())
                                intent.putExtra(VariableName.TYPE,"1")
                                startActivity(intent)
                            }
                            1->{
                                var intent= Intent(this,WithDrawActivity::class.java)
                                intent.putExtra("data",tv_total.text.toString())
                                intent.putExtra(VariableName.TYPE,"2")
                                startActivity(intent)
                            }
                        }
                    }
                    .setNegative("取消", null)
                    .show(supportFragmentManager)



        }



        swipe.setOnRefreshListener(this)
        getData()



    }

    override fun initData() {

    }

    override fun onRefresh() {
        getData()
    }

    fun getData(){
        App.app().bpService.getMoneyFlow(App.app().user.userId)
                .compose(RxUtil.io2mainLoad(this))
                .subscribe(

                        Consumer {
                            datas.clear()
                            datas.addAll(it.data.money_flow)
                            adapter?.notifyDataSetChanged()
                            swipe.isRefreshing=false
                        },
                        Consumer {
                            RxUtil.errorException(it)
                        }
                )

        App.app().bpService.getUserWallet(App.app().user.userId)
                .compose(RxUtil.io2main())
                .subscribe(
                        Consumer {
                            NumAnimUtils.startAnim(tv_total,it.data.user_wallet.usable_balance.toFloat())
//                            tv_total.setText(it.data.user_wallet.usable_balance)
                            tv_today.setText("今日收入 +"+it.data.user_wallet.user_today_in)
                            tv_commission.setText("总佣金 +"+it.data.user_wallet.user_total_commission)
                        },
                        Consumer {
                            RxUtil.errorException(it)
                        }
                )
    }

}
